/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2012-2-9
 */
package com.autotag.data.extract;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.autotag.dao.AbstractProviderDao;
import com.autotag.dao.ServiceDao;
import com.autotag.entity.AbstractProvider;
import com.autotag.entity.Service;

import fanglu.util.FileDealer;
import fanglu.util.SQLDealer;

public class ServiceListExtractor {
	private static Pattern serviceItemPattern = Pattern.compile(
			"<a href=\"(.+?)\"><span class=\"underline\">(.*?)</span>", Pattern.CASE_INSENSITIVE);
	
	public static List<Service> extractServiceListFromProviderFile(String pageContent){
		ArrayList<Service> resultList = new ArrayList<Service>();
		Matcher matcher = serviceItemPattern.matcher(pageContent);
		while (matcher.find()){
//			String thisItem = new String(pageContent.substring(matcher.start(), matcher.end()));
//			System.out.println(thisItem);
			String ref = matcher.group(1);
			String name = matcher.group(2);
			Service thisService = new Service(-1, name, ref, "", "", Service.ABSTRACT_SERVICE, null);
			resultList.add(thisService);
		}
		return resultList;
	}
	
	public static boolean insertServiceList(Connection conn, List<Service> serviceList){
		boolean result = true;
		Iterator<Service> itor = serviceList.iterator();
		while (itor.hasNext()){
			result = ServiceDao.insertService(conn, itor.next()) & result;
		}
		return result;
	}
	
	public static boolean processServiceListFromFile(Connection conn,
			String fileName, String encoding){
		boolean result = true;
		try {
			String content = FileDealer.getFileContent(fileName, encoding);
			List<Service> serviceList = extractServiceListFromProviderFile(content);
			result = insertServiceList(conn, serviceList);
		} catch (IOException e) {
			e.printStackTrace();
			result = false;
		}
		return result;
	}
	
	public static void processAllServiceListFile(String baseDir,
			String encoding, String dbURL, String user, String pwd){
		Connection conn = SQLDealer.getDbConnection(dbURL, user, pwd);
		List<AbstractProvider> pList = AbstractProviderDao.getAllAbstractProvider(conn);
		Iterator<AbstractProvider> itor = pList.iterator();
		int providerIndex = 0;
		while (itor.hasNext()){
			AbstractProvider thisProvider = itor.next();
			++ providerIndex;
			if (thisProvider.getStatus() == AbstractProvider.SERVICE_LIST_PROVIDER){
				System.out.println(providerIndex + "/" + pList.size() + " is already exist! Provider id is " + thisProvider.getId());
			}
			
			String thisLowerName = thisProvider.getName().toLowerCase();
			String subDirName = "/other";
			if (thisLowerName.toLowerCase().charAt(0) >= 'a' && thisLowerName.toLowerCase().charAt(0) <= 'z'){
				subDirName = "/" + thisLowerName.toLowerCase().charAt(0);
			}
			String thisDirName = baseDir + subDirName + "/" + thisProvider.getName();
			File thisDir = new File(thisDirName);
			if (!thisDir.exists()){
				System.out.println("error: dir not exist/" + thisProvider.getId() + "/" + thisProvider.getRef());
				continue;
			}
			File[] fileList = thisDir.listFiles();
			int serviceNum = 0;
			boolean insertResult = true;
			for (int i=0; i<fileList.length; ++i){
				File thisFile = fileList[i];
				if (!thisFile.getName().contains("provider.htm")){
					try {
						String content = FileDealer.getFileContent(thisFile.getAbsolutePath(), encoding);
						List<Service> serviceList = extractServiceListFromProviderFile(content);
						if (insertServiceList(conn, serviceList)){
							serviceNum += serviceList.size();
						}else{
							insertResult = false;
						}
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}
			
			boolean isUpdateProvider = false;
			if (serviceNum != thisProvider.getServiceNum()){
				thisProvider.setServiceNum(serviceNum);
				isUpdateProvider = true;
			}
			if (insertResult == true){
				thisProvider.setStatus(AbstractProvider.SERVICE_LIST_PROVIDER);
				isUpdateProvider = true;
				
			}
			if (isUpdateProvider){
				AbstractProviderDao.updateProvider(conn, thisProvider);
			}
			if (insertResult == true){
				System.out.println(providerIndex + "/" + pList.size() + " is OK!");
			}else{
				System.out.println(providerIndex + "/" + pList.size() + " is error! Provider id is " + thisProvider.getId());
			}
		}
	}
	
	public static void main(String args[]){
		processAllServiceListFile("d:\\seekda_data\\provider_detail", "utf8",
				"jdbc:mysql://localhost:3306/autotag", "root", "ftc86524");
	}
}
